<template>
  <section>
    <header class="top-tips">
      <span class="num-tip" v-if="fatherComponent === 'home'">第一周</span>
      <span class="num-tip" v-if="fatherComponent === 'item'">题目</span>
    </header>
    <div v-if="fatherComponent === 'home'">
      <div class="item-container-style home-logo"></div>
      <router-link to="item" class="button-style start"></router-link>
    </div>
    <div v-if="fatherComponent === 'item'">
      <div class="item-container-style item-back">
        <div class="item-list-container" v-if="itemDetail && itemDetail.length > 0">
          <header class="item-title">{{ itemDetail[itemNum - 1].topicName}}</header>
          <ul>
            <li class="item-list"
                v-for="(item, index) in itemDetail[itemNum - 1].topicAnswer"
                :key="item.topicId + '' +item.answerId"
                @click="choosed(index, item.answerId)">
              <span :class="['option-style', choosedIndex === index ? 'has-choosed' : '']">{{ chooseType(index) }}</span>
              <span class="option-detail">{{ item.answerName }}</span>
            </li>
          </ul>
        </div>
      </div>
      <span class="button-style next"
            @click="nextItem"
            v-if="itemNum < itemDetail.length"></span>
      <span class="button-style submit"
            @click="submitAnswer"
            v-else></span>
    </div>
  </section>
</template>
<script>
  import {mapGetters, mapActions} from 'vuex'
  import bg from '@/assets/images/1-1.jpg'

  export default {
    name: "item-container",
    props: {
      fatherComponent: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        choosedIndex: null, //选中答案索引
        choosedId: null //选中答案id
      }
    },
    created(){
      //初始化信息
      if(this.fatherComponent === 'home') {
        this.initializeData();
        document.body.style.backgroundImage = `url(${bg})`
      }
    },
    computed: {
      ...mapGetters({
        level: 'getLevel',
        itemNum: 'getItemNum',
        allTime: 'getAllTime',
        timer: 'getTimer',
        itemDetail: 'getItemDetail'
      })
    },
    methods: {
      ...mapActions([
        'addItemNum',
        'initializeData'
      ]),
      /**
       * 下一题
       */
      nextItem() {
        if(this.choosedIndex !== null){
          this.choosedIndex = null;
          // 保存答案, 题目索引加一，跳到下一题
          this.addItemNum(this.choosedId)
        }else{
          alert('您还没有选择答案哦')
        }
      },
      /**
       * 到达最后一题，交卷，请空定时器，跳转分数页面
       */
      submitAnswer() {
        if(this.choosedIndex !== null){
          this.choosedIndex = null;
          this.addItemNum(this.choosedId);
          clearInterval(this.timer);
          this.$router.push('score')
        }else{
          alert('您还没有选择答案哦')
        }
      },
      /**
       * 选中的答案信息
       */
      choosed(type, id) {
        this.choosedIndex = type;
        this.choosedId = id;
      },
      /**
       * 索引0-3对应答案A-B
       */
      chooseType(type) {
        switch (type) {
          case 0:
            return 'A';
          case 1:
            return 'B';
          case 2:
            return 'C';
          case 3:
            return 'D';
        }
      }
    }
  }
</script>


<style lang="less" scoped>
  .top-tips {
    position: absolute;
    height: 7.35rem;
    width: 3.25rem;
    top: -1.3rem;
    right: 1.6rem;
    background: url(../assets/images/WechatIMG2.png) no-repeat;
    background-size: 100% 100%;
    z-index: 10;
    .num-tip {
      position: absolute;
      left: 0.48rem;
      bottom: 1.1rem;
      height: 0.7rem;
      width: 2.5rem;
      font-size: 0.6rem;
      font-family: '黑体', serif;
      font-weight: 600;
      color: #a57c50;
      text-align: center;
    }
  }

  .item-container-style {
    height: 11.625rem;
    width: 13.15rem;
    position: absolute;
    top: 4.1rem;
    left: 1rem;
  }

  .home-logo {
    background: url(../assets/images/1-2.png) no-repeat right center;
    background-size: 13.142rem 100%;
  }

  .item-back {
    background: url(../assets/images/2-1.png) no-repeat;
    background-size: 100% 100%;
    .item-list-container {
      position: absolute;
      height: 7.0rem;
      width: 8.0rem;
      top: 2.4rem;
      left: 3rem;
      -webkit-font-smoothing: antialiased;
      .item-title {
        font-size: 0.65rem;
        color: #fff;
        line-height: 0.7rem;
      }
      .item-list {
        font-size: 0;
        margin-top: 0.4rem;
        width: 10rem;
        span {
          display: inline-block;
          font-size: 0.6rem;
          color: #fff;
          vertical-align: middle;
        }
        .option-style {
          height: 0.725rem;
          width: 0.725rem;
          border: 1px solid #fff;
          border-radius: 50%;
          line-height: 0.725rem;
          text-align: center;
          margin-right: 0.3rem;
          font-size: 0.5rem;
          font-family: 'Arial', serif;
        }
        .has-choosed {
          background-color: #ffd400;
          color: #575757;
          border-color: #ffd400;
        }
        .option_detail {
          width: 7.5rem;
          padding-top: 0.11rem;
        }
      }
    }
  }

  .button-style {
    display: block;
    height: 2.1rem;
    width: 4.35rem;
    position: absolute;
    top: 16.5rem;
    left: 50%;
    margin-left: -2.4rem;
  }

  .start, .next, .submit {
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }

  .start {
    background-image: url(../assets/images/1-4.png);
  }

  .next {
    background-image: url(../assets/images/2-2.png);
  }

  .submit {
    background-image: url(../assets/images/3-1.png);
  }
</style>
